package com.slf.demo.product.controller;

import com.alibaba.druid.pool.DruidDataSource;
import com.slf.demo.config.DynamicDataSource;
import com.slf.demo.product.entity.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author slf
 * @date 2021/7/14
 */
@Controller
@RequestMapping("/person")
public class PersonController {


    /**请求地址: localhost:8080/person/show?name=zhang&age=23 请求方式是get*/
    @RequestMapping(value = "/show",method = RequestMethod.GET)
    /**@RequestParam("name")绑定请求地址中的name到参数name中    ModelMap map 存放返回内容*/
    public String show(@RequestParam("name")String name, @RequestParam("age")int age, ModelMap map){
        Person person= new Person();
        person.setName(name);
        person.setAge(age);
        map.addAttribute("person",person);
        /**返回的是显示数据的html的文件名*/
        return "index";
    }

    @Autowired
    JdbcTemplate jdbcTemplate;

    @ResponseBody
    @RequestMapping(value = "/showPerson",method = RequestMethod.GET)
    public List<Map<String, Object>> show(){
        String sql = "select * from spring_security.sys_user";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
        for(Map<String,Object> m:maps){
            System.out.println(m.toString());
        }
        /**返回的是显示数据的html的文件名*/
        return maps;
    }

    @ResponseBody
    @RequestMapping(value = "/testDataSource",method = RequestMethod.GET)
    public synchronized List<Map<String, Object>> testDataSource(){
        String sql = "SELECT table_name, table_comment, create_time, update_time FROM\n" +
                "information_schema.TABLES WHERE table_schema = ( SELECT DATABASE ())";
        DruidDataSource dataSource =  new DruidDataSource();
        String url = "jdbc:mysql://localhost:3306/vhr?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8";
        dataSource.setUrl(url);
        dataSource.setUsername("root");
        dataSource.setPassword("root");
        String key =  "vhr";
        DynamicDataSource.dataSourceMap.put(key,dataSource);
        DynamicDataSource.refreshDataSource();
        DynamicDataSource.setDataSource(key);
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
        for(Map<String,Object> m:maps){
            System.out.println(m.toString());
        }
        try {
            Thread.sleep(20000);
            System.out.println("testConnect sleep run....");
        }catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("testConnect run....");
        DynamicDataSource.clear();
        /**返回的是显示数据的html的文件名*/
        return maps;
    }

    @ResponseBody
    @RequestMapping(value = "/testDataSource2",method = RequestMethod.GET)
    public List<Map<String, Object>> testDataSource2(){
        String sql = "SELECT table_name, table_comment, create_time, update_time FROM\n" +
                "information_schema.TABLES WHERE table_schema = ( SELECT DATABASE ())";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
        for(Map<String,Object> m:maps){
            System.out.println(m.toString());
        }
        /**返回的是显示数据的html的文件名*/
        return maps;
    }

    @ResponseBody
    @RequestMapping(value = "/testDataSource3",method = RequestMethod.GET)
    public List<Map<String, Object>> testDataSource3(){
        String sql = "SELECT table_name, table_comment, create_time, update_time FROM\n" +
                "information_schema.TABLES WHERE table_schema = ( SELECT DATABASE ())";
        DynamicDataSource.setDataSource("vhr");
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
        try {
            Thread.sleep(10000);
            System.out.println("testConnect3 sleep run....");
        }catch (InterruptedException e) {
            e.printStackTrace();
        }
        DynamicDataSource.clear();
        for(Map<String,Object> m:maps){
            System.out.println(m.toString());
        }

        /**返回的是显示数据的html的文件名*/
        return maps;
    }
}
